package com.jyu.apts.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.QRCodeTemplateDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.dto.TableDto;
import com.jyu.apts.dto.UserDto;
import com.jyu.apts.entity.Organization;
import com.jyu.apts.entity.QRCodeTemplate;
import com.jyu.apts.entity.User;
import com.jyu.apts.service.QRCodeTemplateService;
import com.jyu.apts.service.UserService;

@RestController
public class OrganizationQRCodeTemplateController {

	@Autowired
	QRCodeTemplateService qrCodeTemplateService;
	@Autowired
	UserService userService;
	
	@RequiresPermissions("organizationQRCodeTemplate_html")
	@RequestMapping("/organizationQRCodeTemplate_html")
	public ModelAndView organizationQRCodeTemplate_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/organizationQRCodeTemplate.html");
		return mv;
	}
	
	@RequiresPermissions("organizationQRCodeTemplate_html")
	@RequestMapping("/addOrganizationQRCodeTemplate")
	public ResultDto addQRCodeTemplate(String name,String[] dataHeads,String description,Integer visibility) {
		StringBuilder sb = new StringBuilder("");
		if(dataHeads == null || dataHeads.length == 0) {
			sb.append("#");
		}
		for(String dataHead : dataHeads) {
			sb.append("#");
			sb.append(dataHead);
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		QRCodeTemplate organizationQRCodeTemplate = new QRCodeTemplate();
		organizationQRCodeTemplate.setName(name);
		organizationQRCodeTemplate.setAuthor(user);
		organizationQRCodeTemplate.setDataHeads(sb.toString().substring(1));
		organizationQRCodeTemplate.setDescription(description);
		organizationQRCodeTemplate.setVisibility(visibility);
		qrCodeTemplateService.save(organizationQRCodeTemplate);
		return new ResultDto("添加成功",null,0);
	}
	
	@RequiresPermissions("organizationQRCodeTemplate_html")
	@RequestMapping("/upOrganizationQRCodeTemplate")
	public ResultDto upOrganizationQRCodeTemplate(long id,String[] dataHeads,String name,String description,Integer visibility) {
		QRCodeTemplate organizationQRCodeTemplate = qrCodeTemplateService.getOne(id);
		if(organizationQRCodeTemplate == null) {
			return new ResultDto("二维码模板不存在!",null,2);
		}
		StringBuilder sb = new StringBuilder("");
		if(dataHeads == null || dataHeads.length == 0) {
			sb.append("#");
		}
		for(String dataHead : dataHeads) {
			sb.append("#");
			sb.append(dataHead);
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		if(organizationQRCodeTemplate.getAuthor().getOrganization().getId()!=organization.getId()) {
			return new ResultDto("非本机构创建的二维码模板",null,2);
		}
		organizationQRCodeTemplate.setDescription(description);
		organizationQRCodeTemplate.setName(name);
		organizationQRCodeTemplate.setDataHeads(sb.toString().substring(1));
		organizationQRCodeTemplate.setVisibility(visibility);
		qrCodeTemplateService.save(organizationQRCodeTemplate);
		return new ResultDto("更新成功",null,0);
	}
	
	@RequiresPermissions("organizationQRCodeTemplate_html")
	@RequestMapping("/delOrganizationQRCodeTemplate")
	public ResultDto delOrganizationQRCodeTemplate(String ids) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		String[] idArray = ids.split(",");
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : idArray) {
			l_ids.add(new Long(id));
		}
		List<QRCodeTemplate> organizationQRCodeTemplates = qrCodeTemplateService.findAllById(l_ids);
		for(QRCodeTemplate qrCodeTemplate : organizationQRCodeTemplates){
			if(qrCodeTemplate.getAuthor().getOrganization().getId()!=organization.getId()) {
				return new ResultDto("包含非本机构创建的二维码模板",null,2);
			}
		}	
		
		qrCodeTemplateService.delete(organizationQRCodeTemplates);
		return new ResultDto("删除成功",null,0);
	}
	
	@RequiresPermissions("organizationQRCodeTemplate_html")
	@RequestMapping("/getOrganizationQRCodeTemplatePage")
	public TableDto<QRCodeTemplateDto> getOrganizationQRCodeTemplatePage(Integer pageNumber,Integer pageSize,String search) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		Page<QRCodeTemplate> pageResult = qrCodeTemplateService.getQRCodeTemplatePage(pageNumber, pageSize, organization.getId(), search);
		List<QRCodeTemplate> organizationQRCodeTemplates = pageResult.getContent();
		List<QRCodeTemplateDto> organizationQRCodeTemplateDtos = new ArrayList<QRCodeTemplateDto>();
		organizationQRCodeTemplates.forEach(organizationQRCodeTemplate->organizationQRCodeTemplateDtos.add(new QRCodeTemplateDto().convert(organizationQRCodeTemplate)));
		return new TableDto<QRCodeTemplateDto>(organizationQRCodeTemplateDtos,pageResult.getTotalElements());
	}
	
	@RequiresAuthentication
	@RequestMapping("/getAllOrganizationQRCodeTemplate")
	public List<QRCodeTemplateDto> getAllOrganizationQRCodeTemplate() {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		List<QRCodeTemplateDto> organizationQRCodeTemplateDtos = new ArrayList<QRCodeTemplateDto>();
		List<QRCodeTemplate> organizationQRCodeTemplates = qrCodeTemplateService.findAllByOrganization(organization.getId());
		organizationQRCodeTemplates.forEach(organizationQRCodeTemplate->organizationQRCodeTemplateDtos.add(new QRCodeTemplateDto().convert(organizationQRCodeTemplate)));
		return organizationQRCodeTemplateDtos;
	}
	
}
